jQuery(document).ready(function () {	
	jQuery(".leftmenu ul li").removeClass("current");
	jQuery(".leftmenu ul li a.orders").parent().addClass("current");
	
	jQuery(".apply-change").click(function () {
		var action = jQuery(".action-value").val();
		if(action=="5"){
			var cfirm = confirm("Bạn có chắc chắn muốn huỷ đơn hàng ?");
			if(cfirm){
				multiple_action(action);
			}
		}else if(action=="6"){
			var cfirm = confirm("Bạn có chắc chắn muốn xoá đơn hàng ? Đơn hàng đã xoá không thể phục hồi lại !");
			if(cfirm){
				multiple_action(action);
			}
		}else{
			var cfirm = confirm("Bạn có chắc chắn muốn thực hiện thao tác này ?");
			if(cfirm){
				multiple_action(action);
			}
		}		
	});
	jQuery(".delete_button").click(function () {	
		var cfirm = confirm("Bạn có chắc chắn muốn huỷ đơn hàng này ?");
		if(cfirm){
			showAjaxEffect();
			jQuery.get("/admin/process-order", {ids: jQuery(this).attr("href"), action: "5"}, function(data) {
				hideAjaxEffect();
				location.reload();
			});	
		}			
		return false;
	});
	jQuery(".accept_button").click(function () {	
		var cfirm = confirm("Xác nhận đơn hàng này đã được thanh toán bằng hình thức chuyển khoản ?");
		if(cfirm){
			showAjaxEffect();
			jQuery.get("/admin/process-order", {ids: jQuery(this).attr("href"), action: "11"}, function(data) {
				hideAjaxEffect();
				if(data!=""){
					alert(data);
				}		
				location.reload();
			});	
		}			
		return false;
	});
	jQuery(".finish_button").click(function () {	
		var cfirm = confirm("Xác nhận đơn hàng đã được giao trực tiếp ?");
		if(cfirm){
			showAjaxEffect();
			jQuery.get("/admin/process-order", {ids: jQuery(this).attr("href"), action: "8"}, function(data) {				
				hideAjaxEffect();
				if(data!=""){
					alert(data);
				}		
				location.reload();
			});	
		}			
		return false;
	});	
	jQuery(".inline").click(function () {		
		var orderId = jQuery(this).attr("id");		
		var shipType = jQuery("input[name=" + orderId + "shipType]").val();
		var address = jQuery("input[name=" + orderId + "address]").val();
		var city = jQuery("input[name=" + orderId + "city]").val();
		var district = jQuery("input[name=" + orderId + "district]").val();
		var ward = jQuery("input[name=" + orderId + "ward]").val();	
		var note = jQuery("input[name=" + orderId + "note]").val();	
		if(shipType==1){
			jQuery("#shipType1").attr("checked", "checked");
		}else if(shipType==2){
			jQuery("#shipType2").attr("checked", "checked");
		}
		check_ship_type();	
		loadCityByForm(city, "addressForm");
		loadDistrictByForm(city, district, "addressForm");
		loadWardByForm(district, ward, "addressForm");
		jQuery("input[name=address]").val(address);
		jQuery("form#addressForm select[name=city1]").val(city);
		jQuery("form#addressForm select[name=district1]").val(district);
		jQuery("form#addressForm select[name=ward1]").val(ward);
		jQuery("form#addressForm input[name=order_id]").val(orderId);
		jQuery("form#addressForm textarea[name=note]").val(note);
	});	
	jQuery("#btnSubmit").click(function () {				
		if(jQuery("input[name=shipType]:checked").val()==="1"){			
			var address = jQuery("form#addressForm input[name=address]").val();
			var city = jQuery("form#addressForm select[name=city1]").val();
			var district = jQuery("form#addressForm select[name=district1]").val();
			var ward = jQuery("form#addressForm select[name=ward1]").val();
			if(address==""){			
				jQuery("form#addressForm input[name=address]").addClass("error");			
				jQuery("form#addressForm input[name=address]").parent().find("label").text("Cần nhập thông tin địa chỉ");
				jQuery("form#addressForm input[name=address]").parent().find("label").removeClass("hidden");
				return false;
			}
			if(city==""){
				jQuery("form#addressForm select[name=city1]").addClass("error");
				jQuery("form#addressForm select[name=city1]").parent().find("label").text("Cần chọn tỉnh/thành phố");
				jQuery("form#addressForm select[name=city1]").parent().find("label").removeClass("hidden");
				return false;
			}
			if(district==""){
				jQuery("form#addressForm select[name=district1]").addClass("error");
				jQuery("form#addressForm select[name=district1]").parent().find("label").text("Cần chọn quận/huyện");
				jQuery("form#addressForm select[name=district1]").parent().find("label").removeClass("hidden");
				return false;
			}
			if(ward==""){
				jQuery("form#addressForm select[name=ward1]").addClass("error");
				jQuery("form#addressForm select[name=ward1]").parent().find("label").text("Cần chọn phường/xã");
				jQuery("form#addressForm select[name=ward1]").parent().find("label").removeClass("hidden");
				return false;
			}
			showAjaxEffect();
			jQuery.post("/admin/process-order", { 
				orderId: jQuery("input[name=order_id]").val(), 
				shipType: jQuery("input[name=shipType]:checked", "#addressForm").val(), 
				address: address, 
				city: city,
				district: district,
				ward: ward,
				note: jQuery("form#addressForm textarea[name=note]").val(),
					} ,
				function( data ) {
					hideAjaxEffect();
					location.reload();
			     }
			);	
		}else if(jQuery("input[name=shipType]:checked").val()==="2"){
			showAjaxEffect();
			jQuery.post(
				"/admin/process-order", 
				{ 
					orderId: jQuery("input[name=order_id]").val(), 
					shipType: 2,  
					note: jQuery("form#addressForm textarea[name=note]").val(),
				} ,
				function( data ) {
					hideAjaxEffect();
					location.reload();
			     }
			);
		}
		
	});	
	jQuery("input[name=shipType]").click(function(){		
		check_ship_type();
	});
	jQuery("#btnReset").click(function(){		
		refreshOrderSearch();
	});
});
function check_ship_type(){
	if(jQuery("input[name=shipType]:checked").val()==="1"){			
		jQuery("form#addressForm input[name=address]").attr("readonly", false);
		jQuery("form#addressForm input[name=address]").removeAttr("style");
		jQuery("form#addressForm select[name=city1]").attr("readonly", false);
		jQuery("form#addressForm select[name=city1]").removeAttr("style");
		jQuery("form#addressForm select[name=district1]").attr("readonly", false);
		jQuery("form#addressForm select[name=district1]").removeAttr("style");
		jQuery("form#addressForm select[name=ward1]").attr("readonly", false);
		jQuery("form#addressForm select[name=ward1]").removeAttr("style");	
	}else if(jQuery("input[name=shipType]:checked").val()==="2"){			
		jQuery("form#addressForm input[name=address]").attr("readonly", true);
		jQuery("form#addressForm select[name=city1]").attr("readonly", true);
		jQuery("form#addressForm select[name=district1]").attr("readonly", true);
		jQuery("form#addressForm select[name=ward1]").attr("readonly", true);
		jQuery("form#addressForm select[name=district1]").attr("style", "opacity:0.5;");
		jQuery("form#addressForm select[name=ward1]").attr("style", "opacity:0.5;");
		jQuery("form#addressForm select[name=city1]").attr("style", "opacity:0.5;");
		jQuery("form#addressForm input[name=address]").attr("style", "opacity:0.5;");
	}
}
function multiple_action(action) {
	var ids = "";
	jQuery("#list-table tbody tr td input[type=checkbox]").each(function (item, data) {
		if (jQuery(this).is(':checked')) {
			ids += jQuery(this).val() + ",";
		}
	});
	showAjaxEffect();
	jQuery.get("/admin/process-order", {ids: ids, action: action}, function(data) {
		hideAjaxEffect();
		location.reload();
	});
}
function refreshOrderSearch() {
	jQuery("form#searchForm select[name=city]").val("");
	jQuery("form#searchForm select[name=district]").val("");
	jQuery("form#searchForm input[name=orderId]").val("");
	jQuery("form#searchForm input[name=email]").val("");
	jQuery("form#searchForm input[name=shipName]").val("");
	jQuery("form#searchForm input[name=shipAddress]").val("");
	jQuery("form#searchForm input[name=shipPhoneNumber]").val("");
	jQuery("form#searchForm input[name=startTime]").val("");
	jQuery("form#searchForm input[name=endTime]").val("");
	jQuery("form#searchForm select[name=status]").val("0");
	jQuery("form#searchForm select[name=paymentType]").val("0");
	jQuery("form#searchForm select[name=checkOut]").val("0");
}